package com.zjh.admin.controller;


import com.zjh.admin.entity.DO.SysMenu;
import com.zjh.admin.service.ISysMenuService;
import com.zjh.admin.vo.DictVO;
import com.zjh.common.entity.Response;
import com.zjh.common.utils.redis.RedisHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/Admin/redis")
public class RedisController {

    private final String key = "redis:key:test";

    @Resource
    private RedisHelper redisHelper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private ISysMenuService sysMenuService;

    @GetMapping("get")
    public Object test1(@RequestParam String key) {
        return redisHelper.getCacheObject(key);
    }

    @PostMapping("put")
    public Response test2(@RequestBody DictVO dictVO) {
        redisHelper.setCacheObject(dictVO.getKey(), dictVO.getData());
        return Response.success();
    }

    @GetMapping("test1")
    public Response<List<SysMenu>> test1() throws InterruptedException {

//        Object cacheObject = redisTemplate.opsForValue().get(key);
//        if(cacheObject != null){
//            sysMenuService.updateData();
//            return Response.success((List<SysMenu>) cacheObject);
//        }else {
//            List<SysMenu> data = sysMenuService.getData();
//            redisTemplate.opsForValue().set(key,data);
//            return Response.success(data);
//        }

        return Response.success(sysMenuService.getData());

    }

}
